<?php


namespace iflow\template\document\Tag;


use iflow\template\config\Config;
use iflow\template\document\abstracts\TagAbstract;
use iflow\template\document\exception\TemplateException;
use iflow\template\document\Parser\DOMNodeParser;
use iflow\template\document\Parser\ParserHtml;
use iflow\template\document\RenderView;

class IncludeTag extends TagAbstract
{

    protected array $hiddenAttributes = [
        "file"
    ];

    protected RenderView $includeDocument;

    public function parser(DOMNodeParser $node, Config $config, ParserHtml $parserHtml): static
    {
        parent::parser($node, $config, $parserHtml); // TODO: Change the autogenerated stub
        $file = $this->config -> getViewRootPath().($this->attributes['file'] ?: "");
        if (!file_exists($file)) {
            throw new TemplateException("include template file not does exists");
        }

        $this->includeDocument = new RenderView(file_get_contents($file));
        // 将导入的HTML 代码解析为 PHPCode
        $this->html = $this->parserHtml -> traverseNodes(
            $this->includeDocument -> getElementsByTagName('library') -> item(0)
        );
        return $this;
    }
}